import { fileURLToPath } from 'node:url';
import { dirname, resolve } from "node:path";
import { defineConfig } from 'vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import tailwindcss from "@tailwindcss/vite";

const __dirname = dirname( fileURLToPath( import.meta.url ) );
const VITE_API = 'http://local.thinkphp.com/index.php/api/';
// https://vite.dev/config/
export default defineConfig( {
	plugins : [
		vue(),
		vueDevTools(),
		tailwindcss(),
		AutoImport( {
			eslintrc : {
				enabled : true,
			},
			include : [
				/\.[tj]sx?$/,
				/\.vue$/,
			],
			imports : [
				'vue',
				'vue-router',
				'pinia',
				'vue-i18n',
			],
			dts : 'types/auto-imports.d.ts',
			dirs : [
				'./src/hooks',
				'./src/store',
				'./src/utils',
				'./src/components',
			],
		} ),
		Components( {
			dts : 'types/components.d.ts',
			resolvers : [ ElementPlusResolver() ],
		} ),
	],
	server : {
		host : '0.0.0.0',
		port : 8080,
		proxy : {
			'/api' : {
				target : VITE_API,
				changeOrigin : true,
				rewrite : path => path.replace( /^\/api/, '' ),
			},
			'/upload' : {
				target : VITE_API,
				changeOrigin : true,
				rewrite : path => path.replace( /^\/upload/, '' ),
			},
		},
	},
	resolve : {
		alias : {
			'@' : resolve( __dirname, 'src' ),
			'@assets' : resolve( __dirname, 'src/assets' ),
			'@components' : resolve( __dirname, 'src/components' ),
			'@views' : resolve( __dirname, 'src/views' ),
			'@utils' : resolve( __dirname, 'src/utils' ),
			'@stores' : resolve( __dirname, 'src/stores' ),
			'@hooks' : resolve( __dirname, 'src/hooks' ),
			'@api' : resolve( __dirname, 'src/api' ),
		},
		extensions : [ '.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue' ],
	},
	envPrefix : 'VITE_',
} )
